#include "main.h"

Player* GlpPlayer = NULL;
Map* GlpMap = NULL;

void ScrollScreen(void)
{
	//load str 0x6185
	char* T = GMUD_LoadData(4, 0x6171, 0x14);
	char* ls;
	string Ts(T);
	delete[] T;
	wstring Title = s2ws(Ts);
	wstring Story[38];
	for(int i = 0; i < 38; ++i)
	{
		ls = GMUD_LoadData(4, 0x6185 + i * 0x14, 0x14);
		string lstr(ls);
		delete[] ls;
		Story[i] = s2ws(lstr);
	}
	Video::Clear();
	Input::ClearKeyStatus();
	for(int y = 33; y > -415; --y)
	{
		for(int i = 0; i < 38; ++i)
		{
			if(y + i * 16 < 0)
				continue;
			if(y + i * 16 > 80)
				break;
			Text::L_DrawOneLine(0, y + i * 16, Story[i]);
		}
		Text::L_DrawOneLine(0, 0, Title);
		Video::Update();
		GmudDelay(125);
		if(Input::State.KeyPress)
			break;
		Video::Clear();
	}
	Video::Clear();
	Input::ClearKeyStatus();
}

void GMUD_GameStart(void)
{
	/*	
	//New Game
	if(NULL == GlpPlayer)
	char *data = new char[0x153];
	if(-1 == ReadSave(data))
	{
		ScrollScreen();
	}
	*/
	GlpPlayer = new Player();
	GlpMap = new Map();
}

void GMUD_GameMain(void)
{
	GMUD_GameStart();
	//Map
	Input::State.dir = kDirUnknown;
	Input::ClearKeyStatus();
	GlpMap->RefreshMap();
	while(1)
	{
		Video::Update();
		if(kDirUnknown != Input::State.dir)
		{
			if(kDirL == Input::State.dir)
				GlpMap->KeyLeft();
			if(kDirR == Input::State.dir)
				GlpMap->KeyRight();
			GlpMap->RefreshMap();
		}
		if(kKeyPgUp & Input::State.Instruction)
		{
			GlpMap->KeyLL();
			continue;
		}
		if(kKeyPgDn & Input::State.Instruction)
		{
			GlpMap->KeyRR();
			continue;
		}
		if(kKeyUp == Input::State.Instruction)
		{
			GlpMap->KeyUp();
			GlpMap->RefreshMap();
		}
		if(kKeyDown == Input::State.Instruction)
		{
			GlpMap->KeyDown();
			GlpMap->RefreshMap();
		}
		if(kKeyEnt & Input::State.Instruction)
			GlpMap->KeyEnter();
		if(kKeyExit & Input::State.Instruction)
			GlpMap->KeyEsc();
		if(kKeyFly & Input::State.Instruction)
			GlpMap->KeyFly();
		Input::ClearKeyStatus();
		GmudDelay(FRAME_TIME);
	}
}
